/*
 * ButtonController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the web-geek, Version 1.0 (the "License");
 *
 */
package org.geek.action.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.geek.action.service.IActionService;
import org.geek.common.base.BaseController;
import org.geek.common.entity.Criteria;
import org.geek.common.entity.ExtGridReturn;
import org.geek.common.entity.ExtPager;
import org.geek.common.entity.ExtReturn;
import org.geek.domain.Action;
import org.geek.domain.Module;
import org.geek.module.service.IModuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
@Controller
@RequestMapping("/action/")
public class ActionController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(ActionController.class);

	@Resource
	private IActionService actionService;
	
	@Resource
	private IModuleService moduleService;
	
	@RequestMapping(value="initPage.jhtml",method=RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request, ModelMap model) {
		model.put("parentId", request.getParameter("parentId"));
		this.log(request, logger, "访问操作列表");
		return "action/action";
	}
	@RequestMapping(value="getDataList.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object getDataList(ExtPager pager,HttpServletRequest request){
		Criteria criteria = new Criteria();
		 List<Module> modules = moduleService.getModuleByCriteria(criteria);
		String moduleId = request.getParameter("moduleId");
		if(StringUtils.isNotEmpty(moduleId)){
			criteria.put("moduleId", moduleId);
		}
		int total = actionService.countActionByCriteria(criteria);
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setEnd(pager.getLimit() + pager.getStart());
			criteria.setStart(pager.getStart());
		}
		List<Action> list = actionService.getActionByCriteria(criteria);
		for(Action action : list){
			for(Module module : modules){
				if(action.getModuleId()==module.getModuleId()){
					action.setModuleText(module.getModuleText());
				}
			}
		}
		return new ExtGridReturn(total, list);
	}
	
	
	@RequestMapping(value="addAction.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object addAction(HttpServletRequest request,Action action){
		boolean success = true;
		
		if(StringUtils.isEmpty(action.getActionName()))success = false;
		
		if(success && actionService.addAction(action)<=0){
			success = false;
		}
		String msg = success?"功能操作添加成功！":"功能操作添加失败！";
		
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="modAction.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object modAction(HttpServletRequest request,Action action){
		boolean success = true;
		
		if(StringUtils.isEmpty(action.getActionName()))success = false;
		
		if(success && actionService.modAction(action)<=0){
			success = false;
		}
		String msg = success?"功能操作修改成功！":"功能操作修改失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="delActionByActionId.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object delActionByActionId(HttpServletRequest request,int id){
		boolean success = true;
		if(actionService.delActionById(id)<=0){
			success = false;
		}
		String msg = success?"功能操作删除成功！":"功能操作删除失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
}
